package com.luoxiang.socialandroid.face.view;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.ACCID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.faceunity.ui.control.FaceBeautyControlView;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.luoxiang.lib_common.bean.ResponseDTO;
import com.luoxiang.lib_common.utils.AESUtil;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.activity.CommunityViedioActivity;
import com.luoxiang.socialandroid.bean.BeforeCallBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.SaveUserBeautyConfigDTO;
import com.luoxiang.socialandroid.bean.UserBeautyConfigBean;
import com.luoxiang.socialandroid.face.DemoConfig;
import com.luoxiang.socialandroid.face.base.BaseFaceUnityActivity;
import com.luoxiang.socialandroid.face.data.FaceBeautyDataFactory;
import com.luoxiang.socialandroid.face.data.disksource.FUUtils;
import com.luoxiang.socialandroid.face.data.disksource.FaceBeautyData;
import com.luoxiang.socialandroid.face.data.source.FaceBeautySource;
import com.luoxiang.socialandroid.face.entity.FunctionEnum;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.netease.yunxin.nertc.ui.CallKitUI;
import com.netease.yunxin.nertc.ui.base.CallParam;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * DESC：美颜
 * Created on 2021/3/1
 */
public class FaceBeautyActivity extends BaseFaceUnityActivity {


    private FaceBeautyControlView mFaceBeautyControlView;
    private FaceBeautyDataFactory mFaceBeautyDataFactory;
    //就这个类 保存从这个类里那数据
    private FaceBeautyData faceBeautyData;
    public static UserBeautyConfigBean userBeautyConfigBean;
    private SaveUserBeautyConfigDTO saveUserBeautyConfigDTO;


    public static boolean needBindDataFactory = false;

    @Override
    public void onResume() {
        if (needBindDataFactory) {
            mFaceBeautyControlView.bindDataFactory(mFaceBeautyDataFactory);
            needBindDataFactory = false;
        }
        super.onResume();
    }

    @Override
    protected int getStubBottomLayoutResID() {
        return R.layout.layout_control_face_beauty;
    }

    //    将StatusBar设置为透明
    public void setStatusBarTranslucent() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this,
                0, null);
        StatusBarUtil.setLightMode(this);
    }

    @Override
    protected void configureFURenderKit() {
        super.configureFURenderKit();
        mFaceBeautyDataFactory.bindCurrentRenderer();
    }

    @Override
    public void initData() {
        super.initData();
        mFaceBeautyDataFactory = new FaceBeautyDataFactory(mFaceBeautyListener);
    }

    @Override
    public void initView() {
        super.initView();
//        setStatusBarTranslucent();
        faceBeautyData = new FaceBeautyData();
        mFaceBeautyControlView = (FaceBeautyControlView) mStubView;
        mFaceBeautyControlView.setResetButton(DemoConfig.IS_SHOW_RESET_BUTTON);
        changeTakePicButtonMargin(getResources().getDimensionPixelSize(com.faceunity.ui.R.dimen.x156), getResources().getDimensionPixelSize(com.faceunity.ui.R.dimen.x166));
    }

    @Override
    public void bindListener() {
        super.bindListener();
        mFaceBeautyControlView.bindDataFactory(mFaceBeautyDataFactory);
        mFaceBeautyControlView.setOnBottomAnimatorChangeListener(showRate -> {
            // 收起 1-->0，弹出 0-->1
            updateTakePicButton(getResources().getDimensionPixelSize(com.faceunity.ui.R.dimen.x166), showRate,
                    getResources().getDimensionPixelSize(com.faceunity.ui.R.dimen.x156), getResources().getDimensionPixelSize(com.faceunity.ui.R.dimen.x256), true);
        });
        bt_preservation.setOnClickListener(view -> {
            FUUtils.buildFaceBeautyData(faceBeautyData, FaceBeautyDataFactory.defaultFaceBeauty, FaceBeautySource.buildFilters(), mFaceBeautyDataFactory.getCurrentStyleIndex());
            if (userBeautyConfigBean != null && userBeautyConfigBean.getId() != null) {
                saveUserBeautyConfigDTO = new SaveUserBeautyConfigDTO(userBeautyConfigBean.getId(), Long.valueOf(MmkvUtil.INSTANCE.decodeInt(ACCID)), 0, new Gson().toJson(faceBeautyData));
            } else {
                saveUserBeautyConfigDTO = new SaveUserBeautyConfigDTO(null, Long.valueOf(MmkvUtil.INSTANCE.decodeInt(ACCID)), 0, new Gson().toJson(faceBeautyData));
            }

            NetUtils.getInstance().getApiService().saveUserBeautyConfig(MmkvUtil.INSTANCE.decodeString(TOKEN), saveUserBeautyConfigDTO)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<NumberBean>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                        }

                        @Override
                        public void onNext(NumberBean numberBean) {
                            String data = numberBean.getData();
                            String decryptStr = null;
                            try {
                                decryptStr = AESUtil.aesDecryptString(data, KEY);
                                Log.i("decryptStr10", decryptStr);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            Gson gson = new Gson();
                            ResponseDTO responseDTO = gson.fromJson(decryptStr, ResponseDTO.class);
                            if (responseDTO == null) {
                                return;
                            }
                            if (responseDTO.getCode() == 0) {
                                Toast.makeText(FaceBeautyActivity.this, getString(R.string.mi_baocunchenggong), Toast.LENGTH_SHORT).show();
                                FUUtils.saveFaceBeautyData2File(faceBeautyData, FaceBeautyDataFactory.defaultFaceBeauty, FaceBeautySource.buildFilters(), mFaceBeautyDataFactory.getCurrentStyleIndex());
                                finish();
                            } else {
                                Toast.makeText(FaceBeautyActivity.this, responseDTO.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onComplete() {
                        }
                    });
        });
    }


    @Override
    protected int getFunctionType() {
        return FunctionEnum.FACE_BEAUTY;
    }

    FaceBeautyDataFactory.FaceBeautyListener mFaceBeautyListener = new FaceBeautyDataFactory.FaceBeautyListener() {

        @Override
        public void onFilterSelected(int res) {
            showToast(res);
        }

        @Override
        public void onFaceBeautyEnable(boolean enable) {
            mCameraRenderer.setFURenderSwitch(enable);
        }
    };


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mFaceBeautyControlView.hideControlView();
        return super.onTouchEvent(event);
    }
}